/**
File Name 	: Time.c
Name, NIM	: Muqtafi Akhmad, 23512021
 **/

#include "Time.h"
// function GetHour (T:Time) -> integer [0..23]; {Return Hour part of T} 

int GetHour(Time T) {
    return T.H;
}
// function GetMin (T:Time) -> integer [0..59]; { Return Minutes part of T } 

int GetMin(Time T) {
    return T.M;
}
// function GetSec (T:Time) -> integer [0..59]; { Return Second part of T } 

int GetSec(Time T) {
    return T.S;
}
// procedure SetHour (input/output T:Time); 

void SetHour(Time *T, int H) {
    T->H = H;
}
// procedure SetMin (input /output T:Time); 

void SetMin(Time *T, int M) {
    T->M = M;
}
// procedure SetSec (input /output T:Time); 

void SetSec(Time *T, int S) {
    T->S = S;
}
// boolean IsValidTime (H,M,S);
// {H,M,S are valid value for composing a Time }

boolean IsValidTime(int H, int M, int S) {
    return (((H >= 0) && (H <= 23)) && ((M >= 0) && (M <= 59)) && ((S >= 0) && (S <= 59)));
}
// function MakeTime (H,M,S : integer) -> Time
// { precondition : isvalidTime (H,M,S) }
// { return a Time from H,M,S }

Time MakeTime(int H, int M, int S) {
    Time t;
    SetHour(&t, H);
    SetMin(&t, M);
    SetSec(&t, S);
    return t;
}
// boolean EqTime (T1, T2:Time);
// {Return true if T1== T2, i.e T1 and T2 have the same H,M and S }
// {in C language, false ==0 }

boolean EqTime(Time T1, Time T2) {
    return ((GetHour(T1) == GetHour(T2)) && (GetMin(T1) == GetMin(T2)) && (GetSec(T1) == GetSec(T2)));
}
// function NextSec (T:Time} -> Time
// {return 1 second after T }

Time NextSec(Time T) {
    return ToTime(TimeToSecond(T) + 1);
}
// function BeforeSec (T:Time} -> Time
// {return 1 second before T } 

Time BeforeSec(Time T) {
    return ToTime(TimeToSecond(T) - 1);
}
// function TimeLessThan(T1, T2 : Time) -> boolean
// returns true if T1 < T2

boolean TimeLessThan(Time T1, Time T2) {
    return (TimeToSecond(T1) < TimeToSecond(T2));
}
// function AddTime (T1,T2: Time}-> Time 
// {return T1 + T2 } 

Time AddTime(Time T1, Time T2) {
    return ToTime(TimeToSecond(T1) + TimeToSecond(T2));
}
// procedure AccTime (input/OutputT1:Time, input T2: Time} 
// {I.S. T1 is defined } 
// {F.S. T1 = T1 + T2 }

void AccTime(Time *T1, Time T2) {
    Time temp = AddTime((*T1), T2);
    SetHour(T1, GetHour(temp));
    SetMin(T1, GetMin(temp));
    SetSec(T1, GetSec(temp));
}
// function TimeToSecond(Time T) -> long int
// converts a time to seconds

long int TimeToSecond(Time T) {
    return ((GetHour(T) * 3600) + (GetMin(T) * 60) + (GetSec(T)));
}
// function ToTime(long int second) -> Time
// converts second in long integer to time

Time ToTime(long int second) {
    second %= 86400;
    Time t;
    SetHour(&t, second / 3600);
    second -= (GetHour(t) * 3600);
    SetMin(&t, second / 60);
    second -= (GetMin(t) * 60);
    SetSec(&t, second);
    return t;
}
